// 企业微信侧边栏应用授权
export const appAgentConfig = {
  data() {
    return {
      agentConfigUrl: null,
      isSignWorkApp: false, // 企业微信应用授权
      agentData: {
        // 企业微信config接口配置
        agent_secret: "",
        agentid: "",
        corpid: ""
      },
      get_token: {},
      appjsApiList: [], // 成功调用agentConfig后 需要调用的接口名称
      appSignCallback: null, // 成功调用agentConfig后 需要调用的回调函数
      appSignError: null, // 调用agentConfig 失败的回调函数
      agentConfigLoading: 0//0加载中，1成功，-1失败
    };
  },

  created() {
    // 需要在 调用getAgentConfig之前设置 appjsApiList,appSignSuccess,appSignError
    if (this.fun.getKey("crop_id")) {
      let corp_id = this.fun.getKey("crop_id");
      window.localStorage.setItem("crop_id", corp_id);
      this.corp_id = corp_id;
    } else {
      this.corp_id = window.localStorage.getItem("crop_id") || '0';
    }
  },

  activated() {
    console.log(this.corp_id);
    if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
      this.getAgentConfig();
    }
    
  },

  methods: {
    // 企业微信配置信息
    async getAgentConfig() {
      let _this = this;
      let getUrl = this.agentConfigUrl || "plugin.wechat-chat-sidebar.frontend.controller.index.get-config";
      let initUrl = this.fun.isIosOrAndroid() === "ios" ? window.initUrl : document.location.href;
      try {
        let response = await $http.post(getUrl,
          { crop_id: this.corp_id, url: initUrl },
          "loading");
        if (response.result == 1) {
          this.agentData = response.data;
        } else {
          this.agentConfigLoading = -1;
          return this.$toast(response.msg);
        }
        this.agentConfigLoading = 0;
        this.get_token = this.agentData.get_token;
        let config = {
          debug: false,
          beta: true,
          jsApiList: ["agentConfig", "closeWindow"],
          openTagList: ["ww-open-data"],
          appId: this.agentData.corpid,
          nonceStr: this.get_token.noncestr,
          timestamp: this.get_token.timestamp,
          signature: this.get_token.token
        };
        wx.config(config);
        wx.ready(() => {
          setTimeout(() => {
            this.getAgentSign(config, this.get_token.app_token);
          }, 500);
        });
        wx.error(res => {
          _this.$toast(JSON.stringify(res));//测试用，没有错误出现提示
          console.log("getAgentConfig：：config信息验证失败！" + JSON.stringify(res));
        });
      } catch (err) {
        this.$toast(err);
        console.log("getAgentConfig接口报错", err);
      }
    },

    // 企业微信 自建应用授权配置
    getAgentSign(config, app_token) {
      let that = this;
      wx.agentConfig({
        debug: false,
        corpid: config.appId,
        agentid: this.agentData.agentid,
        timestamp: config.timestamp,
        nonceStr: config.nonceStr,
        signature: app_token,
        jsApiList: that.appjsApiList,
        success: () => {
          console.log("应用授权完成");
          that.isSignWorkApp = true;
          that.agentConfigLoading = 1;
          that.appSignSuccess && that.appSignSuccess();
          // 测试需删除
          // that.$toast("企业应用授权成功");
        },
        fail: res => {
          console.log("agentConfig::失败", res);
          that.agentConfigLoading = -1;
          that.appSignError && that.appSignError();
          if (res.errMsg.indexOf("function not exist") > -1) {
            that.$toast("版本过低请升级");
          } else {
            that.$toast("企业应用授权失败，请联系管理员重新配置！");
          }
        }
      });
    }
  }
};
